#!/usr/bin/python

import sys, os, time, pdb
from daemon import get_time, get_direction, get_srcIP, get_dstIP, get_srcPort, get_dstPort, \
                   get_protocol, get_rulePort, get_ruleProtocol, get_action
import MySQLdb
from os.path import isfile
from pdb import set_trace


#LOGFILE = "/mnt/hgfs/shared/logger/kern.log"  
#LOGFILE = "/home/ubuntu/Desktop/ipcontext/logger/kern.log"
LOGFILE = "kern.log"
#LOGFILE = "/var/log/kern.log"
CFGFILE = "logger.cfg"

def testSQL():
    try:
        conn = MySQLdb.connect (host = "localhost", user = "root", passwd = "", db = "FW")
        #conn = MySQLdb.connect (host = "localhost", user = "root", passwd = "ubuntu", db = "fw")
        cursor = conn.cursor () 
        create_table(cursor)
        fill_table(cursor)
        print_table(cursor)
        cursor.close()
        conn.close()
    except MySQLdb.Error, e:
        print "Error %d: %s" % (e.args[0], e.args[1])
        sys.exit (1)  
    

def create_table(cursor):
    try:

        cursor.execute ("DROP TABLE IF EXISTS test_log")
        cursor.execute ("""
        CREATE TABLE test_log
        (
            time            CHAR(40),
            direct          CHAR(40),
            sourceIP        CHAR(40),
            destIP          CHAR(40),
            sourcePort      CHAR(40),
            destPort        CHAR(40),
            protocol        CHAR(40),
            rulePort        CHAR(40),
            ruleProtocol    CHAR(40),
            action          CHAR(40)
        )
        """)                              
    except MySQLdb.Error, e:
        print "Error %d: %s" % (e.args[0], e.args[1])
        sys.exit (1)  

        
def fill_table(cursor):
    try:
        cursor.execute ("""
        INSERT INTO test_log (time, direct, sourceIP, destIP, sourcePort, destPort, protocol, rulePort, ruleProtocol, action)
        VALUES ('2007-01-16','Out','1.1.1.1','2.2.2.2','1','2','my_protocol','3','my_protocol','Deny')
        """)
    except MySQLdb.Error, e:
        print "Error %d: %s" % (e.args[0], e.args[1])
        sys.exit (1)
        
def print_table(cursor):    
        try:
            # perform a fetch loop using fetchall()
            cursor.execute ("SELECT * FROM test_log")
            rows = cursor.fetchall ()
            for row in rows:
                print "time = %s" %row[0]
                print "direction = %s" %row[1]
                print "srcIP = %s" %row[2]
                print "dstIP = %s" %row[3]
                print "protocol = %s" %row[4]
                print "srcPort = %s" %row[5]
                print "dstPort = %s" %row[6]
                print "rulePort = %s" %row[7]
                print "ruleProtocol = %s" %row[8]
                print "action = %s" %row[9]
                print "-------------------------------------------"

            print "Number of rows returned: %d" % cursor.rowcount

        except MySQLdb.Error, e:
            print "Error %d: %s" % (e.args[0], e.args[1])
            sys.exit (1)
    
    
def test_parser():
    
    fin = open(LOGFILE,"r")
	
    if isfile(CFGFILE):
        fcfg = open(CFGFILE, "r")
        cfg = fcfg.readline().strip()
        fcfg.close()

        line = fin.readline().strip()
        while line:
            if cfg == line:
                break
            line = fin.readline().strip()

        if not line:
            fin.seek(0,0)
		
    
   
    while 1:
        #time.sleep(0.1)
        line = fin.readline().strip()
        if line:
            fcfg = open(CFGFILE, "w")
            print >> fcfg, line
            fcfg.close()

        if "OSLFW" in line:  
            log_fields = line.split()
            if (len(log_fields) != 18) and (len(log_fields) != 24):
                continue
                
            _time         = get_time(log_fields)
            _direction    = get_direction(log_fields)
            _srcIP        = get_srcIP(log_fields)
            _dstIP        = get_dstIP(log_fields)
            _srcPort      = get_srcPort(log_fields)
            _dstPort      = get_dstPort(log_fields)
            _protocol     = get_protocol(log_fields)
            _rulePort     = get_rulePort(log_fields)
            _ruleProtocol = get_ruleProtocol(log_fields)
            _action       = get_action(log_fields)

            print "time = %s" %_time
            print "direction = %s" %_direction
            print "srcIP = %s" %_srcIP
            print "dstIP = %s" %_dstIP
            print "protocol = %s" %_protocol
            print "srcPort = %s" %_srcPort
            print "dstPort = %s" %_dstPort
            print "rulePort = %s" %_rulePort
            print "ruleProtocol = %s" %_ruleProtocol
            print "action = %s" %_action
            print "-------------------------------------------"
            
            

if __name__ == "__main__":
    test_parser()
    # testSQL()
    
